//
//  BankSelectView.m
//  Test
//
//  Created by figs on 15/11/21.
//  Copyright © 2015年 luoqj. All rights reserved.
//

#import "BankSelectView.h"
@interface BankSelectView()<UIPickerViewDelegate, UIPickerViewDataSource>{
    UIPickerView *bankPicker;
    UIView *contentView;
}

@end
@implementation BankSelectView
@synthesize Bank;
+ (instancetype)showInView:(UIView *)view{
    BankSelectView *tmp = [[BankSelectView alloc] initWithFrame:view.bounds];
    [view addSubview:tmp];
    [tmp show];
    return tmp;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadViews];
    }
    return self;
}

- (void)loadViews{
    self.alpha = 0;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 350)];
    [self addSubview:contentView];
    
    //add btn to contentView...
    
    bankPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 300)];
    bankPicker.delegate = self;
    bankPicker.dataSource = self;
    [contentView addSubview:bankPicker];
    bankNamesArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BankAndType.plist" ofType:nil]];//银行类型
    banktypesArray = [[bankNamesArray objectAtIndex:1] objectForKey:@"Type"];//银行卡
    
    //初始化默认数据
    self.Bank=[[SelectBank alloc] init];
    self.Bank.bankName=[[banktypesArray objectAtIndex:0]objectForKey:@"name"];
    self.Bank.bankType=[[bankNamesArray objectAtIndex:0]objectForKey:@"Bank"];
}

- (void)show{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:5 options:0 animations:^{
        self.alpha = 1;
        contentView.transform = CGAffineTransformMakeTranslation(0, -bankPicker.frame.size.height);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:5 options:0 animations:^{
        self.alpha = 0;
        contentView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{
    [self hide];
}

#pragma mark - picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//    return 2;
    switch (component) {
        case 0:
            return [banktypesArray count];
           
            break;
        case 1:
            return [bankNamesArray count];
            
            break;
        default:
            return 0;
            break;
    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return @"1";
    switch (component) {
        case 0:
            return [[banktypesArray objectAtIndex:row] objectForKey:@"name"];
            //            return @"11";
            break;
        case 1:
            return [[bankNamesArray objectAtIndex:row] objectForKey:@"Bank"];
            //             return @"22";
            break;
        default:
            return nil;
            break;
    }

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
}
@end
